home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / dvips / RCS / dvips.c,v < prev    next >
Encoding:
Text File  |  1990-11-20  |  13.1 KB  |  476 lines

  1. head     1.3;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.3
  10. date     90.11.19.23.28.08;  author tve;  state Exp;
  11. branches ;
  12. next     1.2;
  13.  
  14. 1.2
  15. date     90.11.10.01.00.34;  author tve;  state Exp;
  16. branches ;
  17. next     1.1;
  18.  
  19. 1.1
  20. date     90.11.10.00.28.52;  author tve;  state Exp;
  21. branches ;
  22. next     ;
  23.  
  24.  
  25. desc
  26. @original
  27. @
  28.  
  29.  
  30. 1.3
  31. log
  32. @fixed -P option
  33. @
  34. text
  35. @/*
  36.  *   This is the main routine.
  37.  */
  38. #ifndef DEFRES
  39. #define DEFRES (300)
  40. #endif
  41. #ifndef DEFPFMT
  42. #define DEFPFMT "letter"
  43. #endif
  44.  
  45. #include "structures.h" /* The copyright notice in that file is included too! */
  46. /*
  47.  *   First we define some globals.
  48.  */
  49. fontdesctype *fonthead ;      /* list of all fonts mentioned so far */
  50. fontdesctype *curfnt ;        /* the currently selected font */
  51. sectiontype *sections ;       /* sections to process document in */
  52. Boolean manualfeed ;          /* manual feed? */
  53. Boolean compressed ;          /* compressed? */
  54. int collatedcopies = 1 ;      /* how many collated copies? */
  55. int sectioncopies = 1 ;       /* how many times to repeat each section? */
  56. shalfword linepos = 0 ;       /* where are we on the line being output? */
  57. integer maxpages ;            /* the maximum number of pages */
  58. Boolean notfirst ;            /* true if a first page was specified */
  59. Boolean sendcontrolD ;        /* should we send a control D at end? */
  60. integer firstpage ;           /* the number of the first page if specified */
  61. int numcopies ;               /* number of copies of each page to print */
  62. char *oname ;                 /* output file name */
  63. char *iname ;                 /* dvi file name */
  64. char strings[STRINGSIZE] ;    /* strings for program */
  65. char *nextstring, *maxstring ; /* string pointers */
  66. FILE *dvifile, *bitfile ;     /* dvi and output files */
  67. quarterword *curpos ;         /* current position in virtual character packet */
  68. quarterword *curlim ;         /* final byte in virtual character packet */
  69. fontmaptype *ffont ;          /* first font in current frame */
  70. real conv ;                   /* conversion ratio, pixels per DVI unit */
  71. real alpha ;                  /* conversion ratio, DVI unit per TFM unit */
  72. integer mag ;                 /* the magnification of this document */
  73. Boolean overridemag ;         /* substitute for mag value in DVI file? */
  74. int actualdpi = DEFRES ;      /* the actual resolution of the printer */
  75. int maxdrift ;                /* max pixels away from true rounded position */
  76. char *paperfmt = DEFPFMT ;    /* paper format */
  77. integer fontmem ;             /* memory remaining in printer */
  78. integer pagecount ;           /* page counter for the sections */
  79. integer pagenum ;             /* the page number we currently look at */
  80. long bytesleft ;              /* number of bytes left in raster */
  81. quarterword *raster ;         /* area for raster manipulations */
  82. integer hh, vv ;              /* horizontal and vertical pixel positions */
  83. char *tfmpath ;               /* pointer to directories for tfm files */
  84. char *pkpath ;                /* pointer to directories for pk files */
  85. char *vfpath ;                /* pointer to directories for vf files */
  86. char *figpath ;               /* pointer to directories for figure files */
  87. integer swmem ;               /* font memory in the PostScript printer */
  88. int quiet ;                   /* should we only print errors to stderr? */
  89. int filter ;                  /* act as filter default output to stdout,
  90.                                                default input to stdin? */
  91. int totalpages = 0 ;          /* total number of pages */
  92. Boolean reverse ;             /* are we going reverse? */
  93. Boolean usesPSfonts ;         /* do we use local PostScript fonts? */
  94. Boolean usesspecial ;         /* do we use \special? */
  95. Boolean headers_off ;         /* do we send headers or not? */
  96. char *headerfile ;            /* default header file */
  97. Boolean multiplesects ;       /* more than one section? */
  98. Boolean disablecomments ;     /* should we suppress any EPSF comments? */
  99. char *printer ;               /* what printer to send this to? */
  100. char *mfmode ;                /* default MF mode */
  101. frametype frames[MAXFRAME] ;  /* stack for virtual fonts */
  102.  
  103. #ifdef DEBUG
  104. integer debug_flag = 0;
  105. #endif /* DEBUG */
  106. /*
  107.  *   This routine calls the following externals:
  108.  */
  109. extern void outbangspecials() ;
  110. extern void prescanpages() ;
  111. extern void initprinter() ;
  112. extern void cleanprinter() ;
  113. extern void dosection() ;
  114. extern void getdefaults() ;
  115. extern void cmdout() ;
  116. extern void numout() ;
  117. extern void add_header() ;
  118. extern char *strcpy() ;
  119. /*
  120.  *   This error routine prints an error message; if the first
  121.  *   character is !, it aborts the job.
  122.  */
  123. static char *progname ;
  124. void
  125. error(s)
  126.     char *s ;
  127. {
  128.    (void)fprintf(stderr, "%s: %s\n", progname, s) ;
  129.    if (*s=='!') {
  130.       if (bitfile != NULL) {
  131.          cleanprinter() ;
  132.       }
  133.       exit(1) ; /* fatal */
  134.    }
  135. }
  136.  
  137. /*
  138.  *   Initialize sets up all the globals and data structures.
  139.  */
  140. void
  141. initialize()
  142. {
  143.    fonthead = NULL ;
  144.    sections = NULL ;
  145.    maxpages = 100000 ;
  146.    firstpage = 0 ;
  147.    notfirst = 0 ;
  148.    overridemag = 0 ;
  149.    numcopies = 1 ;
  150.    nextstring = strings ;
  151.    iname = strings ;
  152.    *nextstring++ = 0 ;
  153.    maxstring = strings + STRINGSIZE - 200 ;
  154.    bitfile = NULL ;
  155.    bytesleft = 0 ;
  156.    pkpath = PKPATH ;
  157.    vfpath = VFPATH ;
  158.    tfmpath = TFMPATH ;
  159.    figpath = FIGPATH ;
  160.    swmem = SWMEM ;
  161.    oname = OUTPATH ;
  162.    sendcontrolD = 0 ;
  163.    multiplesects = 0 ;
  164.    disablecomments = 0 ;
  165.    maxdrift = -1 ;
  166. }
  167. /*
  168.  *   This routine copies a string into the string `pool', safely.
  169.  */
  170. char *
  171. newstring(s)
  172.    char *s ;
  173. {
  174.    int l ;
  175.  
  176.    if (s == NULL)
  177.       return(NULL) ;
  178.    l = strlen(s) ;
  179.    if (nextstring + l >= maxstring)
  180.       error("! out of string space") ;
  181.    (void)strcpy(nextstring, s) ;
  182.    s = nextstring ;
  183.    nextstring += l + 1 ;
  184.    return(s) ;
  185. }
  186. /*
  187.  *   Finally, our main routine.
  188.  */
  189. main(argc, argv)
  190.     int argc ;
  191.     char *argv[] ;
  192. {
  193.    int i, lastext = -1 ;
  194.    register sectiontype *sects ;
  195.    char *getenv();
  196.  
  197.    progname = argv[0] ;
  198.    initialize() ;
  199.  
  200.    printer = getenv("PRINTER");
  201.    getdefaults("ps") ;
  202.    if(printer)
  203.       getdefaults(printer);
  204.  
  205. /*
  206.  *   This next whole big section of code is straightforward; we just scan
  207.  *   the options.  An argument can either immediately follow its option letter
  208.  *   or be separated by spaces.  Any argument not preceded by '-' and an
  209.  *   option letter is considered a file name; the program complains if more
  210.  *   than one file name is given, and uses stdin if none is given.
  211.  */
  212.    for (i=1; i<argc; i++) {
  213.       if (*argv[i]=='-') {
  214.          char *p=argv[i]+2 ;
  215.          char c=argv[i][1] ;
  216.          switch (c) {
  217. case 'c' :
  218.             if (*p == 0 && argv[i+1])
  219.                p = argv[++i] ;
  220.             if (sscanf(p, "%d", &numcopies)==0)
  221.                error("! Bad number of copies option (-c).") ;
  222.             break ;
  223. #ifdef DEBUG
  224. case 'd' :
  225.         if (*p == 0 && argv[i+1])
  226.            p = argv[++i];
  227.         if (sscanf(p, "%d", &debug_flag)==0)
  228.            error("! Bad debug option (-d).");
  229.         break;
  230. #endif /* DEBUG */
  231. case 'e' :
  232.             if (*p == 0 && argv[i+1])
  233.                p = argv[++i] ;
  234.             if (sscanf(p, "%d", &maxdrift)==0 || maxdrift<0)
  235.                error("! Bad maxdrift option (-e).") ;
  236.             break ;
  237. case 'f' :
  238.             filter = (*p != '0') ;
  239.             if (filter)
  240.                oname = "" ;
  241.             break ;
  242. case 'h' : case 'H' :
  243.             if (*p == 0 && argv[i+1])
  244.                p = argv[++i] ;
  245.             if (strcmp(p, "-") == 0)
  246.                headers_off = 1 ;
  247.             else
  248.                add_header(p) ;
  249.             break ;
  250. case 'm' :
  251.             manualfeed = (*p != '0') ;
  252.             break ;
  253. case 'n' :
  254.             if (*p == 0 && argv[i+1])
  255.                p = argv[++i] ;
  256. #ifdef SHORTINT
  257.             if (sscanf(p, "%ld", &maxpages)==0)
  258. #else    /* ~SHORTINT */
  259.             if (sscanf(p, "%d", &maxpages)==0)
  260. #endif    /* ~SHORTINT */
  261.                error("! Bad number of pages option (-n).") ;
  262.             break ;
  263. case 'o' : case 'O' :
  264.             if (*p == 0 && argv[i+1] && *argv[i+1]!='-')
  265.                p = argv[++i] ;
  266.             oname = p ;
  267.             break ;
  268. case 'p' :
  269.             if (*p == 0 && argv[i+1])
  270.                p = argv[++i] ;
  271. #ifdef SHORTINT
  272.             if (sscanf(p, "%ld", &firstpage)==0)
  273. #else    /* ~SHORTINT */
  274.             if (sscanf(p, "%d", &firstpage)==0)
  275. #endif    /* ~SHORTINT */
  276.                error("! Bad first page option (-p).") ;
  277.             notfirst = 1 ;
  278.             break ;
  279. case 'q' : case 'Q' :
  280.             quiet = (*p != '0') ;
  281.             break ;
  282. case 'r' :
  283.             reverse = (*p != '0') ;
  284.             break ;
  285. case 't' :
  286.             if (*p == 0 && argv[i+1])
  287.                p = argv[++i] ;
  288.             paperfmt = p ;
  289.             break ;
  290. case 'x' :
  291.             if (*p == 0 && argv[i+1])
  292.                p = argv[++i] ;
  293.             if (sscanf(p, "%d", &mag)==0 || mag < 10 ||
  294.                        mag > 100000)
  295.                error("! Bad magnification parameter (-x).") ;
  296.             overridemag = 1 ;
  297.             break ;
  298. case 'C' :
  299.             if (*p == 0 && argv[i+1])
  300.                p = argv[++i] ;
  301.             if (sscanf(p, "%d", &collatedcopies)==0)
  302.                error("! Bad number of collated copies option (-C).") ;
  303.             break ;
  304. case 'D' :
  305.             if (*p == 0 && argv[i+1])
  306.                p = argv[++i] ;
  307.             if (sscanf(p, "%d", &actualdpi)==0 || actualdpi < 10 ||
  308.                        actualdpi > 10000)
  309.                error("! Bad dpi parameter (-D).") ;
  310.             break ;
  311. case 'F' :
  312.             sendcontrolD = (*p != '0') ;
  313.             break ;
  314. case 'N' :
  315.             disablecomments = (*p != '0') ;
  316.             break ;
  317. case 'P' :
  318.             if (*p == 0 && argv[i+1])
  319.                p = argv[++i] ;
  320.             printer = p ;
  321.         getdefaults("ps") ;
  322.         getdefaults(printer);
  323.             break ;
  324. case 'R' :
  325.             reverse = 0 ;
  326.             break ;
  327. case 'Z' :
  328.             compressed = (*p != '0') ;
  329.             break ;
  330. case '?' :
  331.             (void)fprintf(stderr, BANNER) ;
  332.             break ;
  333. default:
  334.             error("! Bad option, not one of cefhmnopqrtxCDFNPZ?") ;
  335.          }
  336.       } else {
  337.          if (*iname == 0) {
  338.             register char *p ;
  339.  
  340.             lastext = 0 ;
  341.             iname = nextstring ;
  342.             p = argv[i] ;
  343.             while (*p) {
  344.                *nextstring = *p++ ;
  345.                if (*nextstring == '.')
  346.                   lastext = nextstring - iname ;
  347.                else if (*nextstring == '/' || *nextstring == ':')
  348.                   lastext = 0 ;
  349.                nextstring++ ;
  350.             }
  351.             if (lastext == 0) {
  352.                lastext = nextstring - iname ;
  353.                *nextstring++ = '.' ;
  354.                *nextstring++ = 'd' ;
  355.                *nextstring++ = 'v' ;
  356.                *nextstring++ = 'i' ;
  357.             }
  358.             *nextstring++ = 0 ;
  359.          } else
  360.             error("! Two input file names specified.") ;
  361.       }
  362.    }
  363.  
  364.    if (!quiet)
  365.       (void)fprintf(stderr, BANNER) ;
  366.    if (*oname == 0 && ! filter) {
  367.       oname = nextstring ;
  368.       for (i=0; i<=lastext; i++)
  369.          *nextstring++ = iname[i] ;
  370.       *nextstring++ = 'p' ;
  371.       *nextstring++ = 's' ;
  372.       *nextstring++ = 0 ;
  373.    }
  374. #ifdef DEBUG
  375.    if (dd(D_PATHS)) {
  376. #ifdef SHORTINT
  377.     (void)fprintf(stderr,"input file %s output file %s swmem %ld\n",
  378. #else /* ~SHORTINT */
  379.        (void)fprintf(stderr,"input file %s output file %s swmem %d\n",
  380. #endif /* ~SHORTINT */
  381.            iname, oname, swmem) ;
  382.    (void)fprintf(stderr,"tfm path %s pk path %s\n", tfmpath, pkpath) ;
  383.    } /* dd(D_PATHS) */
  384. #endif /* DEBUG */
  385. /*
  386.  *   Now we try to open the dvi file.
  387.  */
  388.    headerfile = (compressed? CHEADERFILE : HEADERFILE) ;
  389.    add_header(headerfile) ;
  390.    if (*iname != 0)
  391.       dvifile = fopen(iname, "r") ;
  392.    else if (filter)
  393.       dvifile = stdin;
  394.    else
  395.       error("! No input filename supplied.") ;
  396.    if (dvifile==NULL)
  397.       error("! DVI file can't be opened.") ;
  398. /*
  399.  *   Now we do our main work.
  400.  */
  401.    if (maxdrift < 0) {
  402.       if (actualdpi <= 599)
  403.          maxdrift = actualdpi / 100 ;
  404.       else if (actualdpi < 1199)
  405.          maxdrift = actualdpi / 200 + 3 ;
  406.       else
  407.          maxdrift = actualdpi / 400 + 6 ;
  408.    }
  409.    prescanpages() ;
  410.    if (usesPSfonts)
  411.       add_header(PSFONTHEADER) ;
  412.    if (usesspecial)
  413.       add_header(SPECIALHEADER) ;
  414.    sects = sections ;
  415.    if (sects == NULL || sects->next == NULL) {
  416.       sectioncopies = collatedcopies ;
  417.       collatedcopies = 1 ;
  418.    } else {
  419.       totalpages *= collatedcopies ;
  420.       multiplesects = 1 ;
  421.    }
  422.    initprinter() ;
  423.    outbangspecials() ;
  424.    for (i=0; i<collatedcopies; i++) {
  425.       sects = sections ;
  426.       while (sects != NULL) {
  427.          if (! quiet)
  428.             (void)fprintf(stderr, ". ") ;
  429.          (void)fflush(stderr) ;
  430.          dosection(sects, sectioncopies) ;
  431.          sects = sects->next ;
  432.       }
  433.    }
  434.    cleanprinter() ;
  435.    if (! quiet)
  436.       (void)fprintf(stderr, "\n") ;
  437.    exit(0) ;
  438.    /*NOTREACHED*/
  439. }
  440. @
  441.  
  442.  
  443. 1.2
  444. log
  445. @Added reasonable handling of -Pprinter option and of $PRINTER
  446. environment variable.
  447. @
  448. text
  449. @d165 6
  450. d287 2
  451. a288 1
  452.             getdefaults(printer) ;
  453. a328 6
  454.  
  455.    if(printer == NULL)
  456.       printer = getenv("PRINTER");
  457.    getdefaults("ps") ;
  458.    if(printer)
  459.       getdefaults(printer);
  460. @
  461.  
  462.  
  463. 1.1
  464. log
  465. @Initial revision
  466. @
  467. text
  468. @d161 1
  469. a164 1
  470.    getdefaults() ;
  471. d281 1
  472. a281 1
  473.             getdefaults() ;
  474. d322 7
  475. @
  476.